//
//  NewsWebCell.m
//  JJHW_coreData
//
//  Created by huazi on 16/4/20.
//  Copyright © 2016年 HFKJ_LZQ. All rights reserved.
//

#import "NewsWebCell.h"

@implementation NewsWebCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self =[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubviews];
    }
    return self;
}
- (void)initSubviews {
    self.webNewsView =[[UIWebView alloc] initWithFrame:CGRectMake(10, 0, DeviceWidth-20, 30)];
    self.webNewsView.delegate = self;
//    self.webNewsView.navigationDelegate = self;
    self.webNewsView.scrollView.scrollEnabled =NO;
    [self.contentView addSubview:self.webNewsView];
}
- (void)reloadDataCell:(id)model AndHeignt:(float)height{
//    NSString *jsString = [[NSString alloc] initWithFormat:@"document.body.style.fontSize=%f;document.body.style.color=%@",13.0,KzxDetailDefaultColor];
//    self.webNewsView.tintColor = KzxDetailDefaultColor;
//    [self.webNewsView stringByEvaluatingJavaScriptFromString:jsString];
    
//    NSString *html = [NSString stringWithFormat:@"%@",@"<html><head></head><body>    <p class=\"MsoNormal\"><a href=\"http://www2.jrhokkaido.co.jp/global/cn/kitaca/index.html\"><span lang=\"EN-US\" style=\"font-size: 12pt; font-family: 'MS Mincho';\">URL'\'Uff1a</span><span style=\"font-family: 'MS Mincho'; font-size: 12pt; line-height: 1.5;\">http://www2.jrhokkaido.co.jp/global/cn/kitaca/index.html(External\nlink</span><span style=\"font-family: 'MS Mincho'; font-size: 12pt; line-height: 1.5;\">)</span></a></p>   </body></html> "];
    [self.webNewsView removeFromSuperview];
    self.webNewsView =nil;
    self.webNewsView =[[UIWebView alloc] initWithFrame:CGRectMake(10, 0, DeviceWidth-20, 30)];
    self.webNewsView.delegate =self;
    self.webNewsView.scrollView.scrollEnabled =NO;
    [self.contentView addSubview:self.webNewsView];
    self.webNewsView.frame =CGRectMake(10, 0, DeviceWidth-20, height);
    [self.webNewsView loadHTMLString:[model objectForKey:@"content"] baseURL:nil];
    
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { // 类似 UIWebView 的 －webViewDidFinishLoad:
    NSLog(@"didFinishNavigation");
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] synchronize];
    
//    CGFloat webViewHeight1=webView.scrollView.contentSize.height;
//    CDLog(@"webView.scrollView.contentSize.height=%f",webView.scrollView.contentSize.height);
//    CGFloat webViewHeight = 2000;
//    CGRect newFrame = webView.frame;
//    
//    newFrame.size.height = webViewHeight;
//    
//    webView.frame = newFrame;
    
//    
//    [webView evaluateJavaScript:@"document.getElementById(\"content\").offsetHeight;"completionHandler:^(id_Nullable result,NSError *_Nullable error) {
//        //获取页面高度，并重置webview的frame
//        CGFloat documentHeight = [result doubleValue];
//        CGRect frame = webView.frame;
//        frame.size.height = documentHeight;
//        webView.frame = frame;
//    }];
    [webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id result,NSError *_Nullable error) {
            //获取页面高度，并重置webview的frame
            CGFloat documentHeight = [result doubleValue];
            CGRect frame = webView.frame;
            frame.size.height = documentHeight;
            webView.frame = frame;
           self.block(documentHeight+20);//+30
    
    }];
    
   
    
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//    NSString *jsString = [[NSString alloc] initWithFormat:@"document.body.style.fontSize=%f;document.body.style.color=%@",18.0,@"#414141"];
////    self.webNewsView.tintColor = KzxDetailDefaultColor;
//    [self.webNewsView stringByEvaluatingJavaScriptFromString:jsString];
    
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] synchronize];
//    
//    CGFloat webViewHeight=[webView.scrollView contentSize].height;
//    
//    CGRect newFrame = webView.frame;
//    
//    newFrame.size.height = webViewHeight;
//    
//    webView.frame = newFrame;
//    self.block(webViewHeight+20);//+30
    
    
    float height = [[self.webNewsView
                            stringByEvaluatingJavaScriptFromString:
                            @"document.body.offsetHeight"] floatValue];
    CDLog(@"webViewCell 的 height=%f",height);
    self.block(height+20);//+30
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType ==UIWebViewNavigationTypeLinkClicked) {
        self.clickBlock(request.URL.absoluteString);
        return NO;
    }
    return YES;
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    // 类似 UIWebView 的 -webView: shouldStartLoadWithRequest: navigationType:
    
    NSLog(@"4.%@",navigationAction.request);
    NSString *url = [navigationAction.request.URL.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    decisionHandler(WKNavigationActionPolicyAllow);
    
    if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
        
        self.clickBlock(navigationAction.request.URL.absoluteString);
    }
    
    
}


@end
